package com.splunk.hiring.imov.entity.convertor;

import java.util.Arrays;
import java.util.List;

import javax.persistence.AttributeConverter;

import org.springframework.stereotype.Component;
import org.thymeleaf.util.StringUtils;

@Component
public class GenreListConvertor implements AttributeConverter<List<String>,String>{
	
	private static final String DELIMITOR = "|";
	
	@Override
	public String convertToDatabaseColumn(List<String> genres) {
		StringBuffer sb = new StringBuffer();
		genres.stream().forEach(genre->sb.append(genre).append(DELIMITOR));
		return sb.substring(0, sb.length()-1);
	}

	public static String staticConvertToDatabaseColumn(List<String> genres) {
		StringBuffer sb = new StringBuffer();
		genres.stream().forEach(genre->sb.append(genre).append(DELIMITOR));
		return sb.substring(0, sb.length()-1);
	}
	
	
	@Override
	public List<String> convertToEntityAttribute(String genres) {
		String[] splits = StringUtils.split(genres, "|");
		return Arrays.asList(splits);
	}
}
